//
//  Conference.m
//  SHPE Conference Companion
//
//  Created by Richard Kirk on 3/25/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "Conference.h"
#import "Location.h"
#import "Session.h"
#import "SessionType.h"
#import "Speaker.h"
#import "Sponsor.h"
#import "SponsorshipLevel.h"
#import "Track.h"
#import "Venue.h"


@implementation Conference
@dynamic id;
@dynamic twitter;
@dynamic name;
@dynamic desc;
@dynamic endDate;
@dynamic thumbnailURL;
@dynamic thumbnailData;
@dynamic startDate;
@dynamic website;
@dynamic email;
@dynamic venues;
@dynamic tracks;
@dynamic sponsors;
@dynamic speakers;
@dynamic location;
@dynamic sponsorshipLevels;
@dynamic sessionTypes;
@dynamic sessions;
@synthesize xmlParser, xmlStringBuilder, dateFormatter;

-(id)initWithParser:(NSXMLParser *)parser
{   
    [super init];
    
    
    xmlParser = parser;
    
    return self;
}

-(id)initWithConferenceId:(int)conferenceId
{
    
    [super initWithEntity:[NSEntityDescription entityForName:@"Conference" inManagedObjectContext:[self managedObjectContext] ] insertIntoManagedObjectContext:[self managedObjectContext]];
    
    self.location = [NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:[self managedObjectContext]];
    
    
    xmlParser = [NSXMLParser alloc];
    xmlParser.delegate = self;
    
//    [xmlParser initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://openshpe.staging.shpe.org/conference/v1/conference/%d", conferenceId]]];    
    
    [xmlParser initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://openshpe.staging.shpe.org/conference/v1/conference/%d?includeAll=true", conferenceId]]];    
    
    
    
    if (xmlParser){
        [xmlParser parse];
    }
    
    [xmlParser release];
    return self;
}

-(NSString *)description
{
    
    NSMutableString *description = [[NSMutableString alloc]init];
    
    [description appendString:[NSString stringWithFormat:@"\nName : %@\nConferenceid : %@\nStart Date : %@\nend Date : %@\nDesc : %@\nemail : %@\nwebsite : %@\nThumbnailURL : %@\ntwitter : %@\naddress : %@\ncity : %@\nprovience : %@\ncountry : %@\npostal code : %@\nlat : %@\nlon : %@\n", self.name, self.id, self.startDate, self.endDate, self.desc, self.email, self.website,self.thumbnailURL, self.twitter, self.location.address, self.location.city, self.location.province, self.location.country, self.location.postalCode, self.location.lat, self.location.lon]];
   
    
    [description appendString:[NSString stringWithString:@"\n-------Session Tracks-------\n"]];
    for (Track * item in self.tracks ) {
        [description appendString:[NSString stringWithFormat:@"--------------------------%@",item ]]; 
    }
    
    [description appendString:[NSString stringWithString:@"\n-------Session Types-------\n"]];
    
    for (SessionType * item in self.sessionTypes ) {
        [description appendString:[NSString stringWithFormat:@"--------------------------%@",item ]]; 
    }
    
    [description appendString:[NSString stringWithString:@"\n-------Venues-------\n"]];
    for (Venue * item in self.venues ) {
        [description appendString:[NSString stringWithFormat:@"--------------------------%@",item ]]; 
    }
    [description appendString:[NSString stringWithString:@"\n-------Speakers-------\n"]];
    for (Track * item in self.speakers ) {
        [description appendString:[NSString stringWithFormat:@"--------------------------%@",item ]]; 
    }
    [description appendString:[NSString stringWithString:@"\n-------Sessions-------\n"]];
    for (Session * item in self.sessions ) {
        [description appendString:[NSString stringWithFormat:@"--------------------------%@",item ]]; 
    }
    [description appendString:[NSString stringWithString:@"\n-------Sponsor Types-------\n"]];
    for (SponsorshipLevel * item in self.sponsorshipLevels ) {
        [description appendString:[NSString stringWithFormat:@"--------------------------%@",item ]]; 
    }
    [description appendString:[NSString stringWithString:@"\n-------Sponsors-------\n"]];
    for (Sponsor * item in self.sponsors ) {
        [description appendString:[NSString stringWithFormat:@"--------------------------%@",item ]]; 
    }


    
    
    
    [description autorelease];
    return description;
    
}

-(void)getAllTracks
{
    
    /*
     
     Set up a method to download all tracks for this conference
     
     */
    
}


- (void)addVenuesObject:(Venue *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"venues" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"venues"] addObject:value];
    [self didChangeValueForKey:@"venues" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeVenuesObject:(Venue *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"venues" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"venues"] removeObject:value];
    [self didChangeValueForKey:@"venues" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addVenues:(NSSet *)value {    
    [self willChangeValueForKey:@"venues" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"venues"] unionSet:value];
    [self didChangeValueForKey:@"venues" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeVenues:(NSSet *)value {
    [self willChangeValueForKey:@"venues" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"venues"] minusSet:value];
    [self didChangeValueForKey:@"venues" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


- (void)addTracksObject:(Track *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"tracks" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"tracks"] addObject:value];
    [self didChangeValueForKey:@"tracks" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeTracksObject:(Track *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"tracks" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"tracks"] removeObject:value];
    [self didChangeValueForKey:@"tracks" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addTracks:(NSSet *)value {    
    [self willChangeValueForKey:@"tracks" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"tracks"] unionSet:value];
    [self didChangeValueForKey:@"tracks" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeTracks:(NSSet *)value {
    [self willChangeValueForKey:@"tracks" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"tracks"] minusSet:value];
    [self didChangeValueForKey:@"tracks" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


- (void)addSponsorsObject:(Sponsor *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sponsors" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sponsors"] addObject:value];
    [self didChangeValueForKey:@"sponsors" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeSponsorsObject:(Sponsor *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sponsors" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sponsors"] removeObject:value];
    [self didChangeValueForKey:@"sponsors" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addSponsors:(NSSet *)value {    
    [self willChangeValueForKey:@"sponsors" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sponsors"] unionSet:value];
    [self didChangeValueForKey:@"sponsors" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeSponsors:(NSSet *)value {
    [self willChangeValueForKey:@"sponsors" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sponsors"] minusSet:value];
    [self didChangeValueForKey:@"sponsors" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


- (void)addSpeakersObject:(Speaker *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"speakers" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"speakers"] addObject:value];
    [self didChangeValueForKey:@"speakers" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeSpeakersObject:(Speaker *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"speakers" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"speakers"] removeObject:value];
    [self didChangeValueForKey:@"speakers" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addSpeakers:(NSSet *)value {    
    [self willChangeValueForKey:@"speakers" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"speakers"] unionSet:value];
    [self didChangeValueForKey:@"speakers" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeSpeakers:(NSSet *)value {
    [self willChangeValueForKey:@"speakers" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"speakers"] minusSet:value];
    [self didChangeValueForKey:@"speakers" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}



- (void)addSponsorshipLevelsObject:(SponsorshipLevel *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sponsorshipLevels" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sponsorshipLevels"] addObject:value];
    [self didChangeValueForKey:@"sponsorshipLevels" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeSponsorshipLevelsObject:(SponsorshipLevel *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sponsorshipLevels" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sponsorshipLevels"] removeObject:value];
    [self didChangeValueForKey:@"sponsorshipLevels" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addSponsorshipLevels:(NSSet *)value {    
    [self willChangeValueForKey:@"sponsorshipLevels" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sponsorshipLevels"] unionSet:value];
    [self didChangeValueForKey:@"sponsorshipLevels" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeSponsorshipLevels:(NSSet *)value {
    [self willChangeValueForKey:@"sponsorshipLevels" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sponsorshipLevels"] minusSet:value];
    [self didChangeValueForKey:@"sponsorshipLevels" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


- (void)addSessionTypesObject:(SessionType *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sessionTypes" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sessionTypes"] addObject:value];
    [self didChangeValueForKey:@"sessionTypes" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeSessionTypesObject:(SessionType *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sessionTypes" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sessionTypes"] removeObject:value];
    [self didChangeValueForKey:@"sessionTypes" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addSessionTypes:(NSSet *)value {    
    [self willChangeValueForKey:@"sessionTypes" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sessionTypes"] unionSet:value];
    [self didChangeValueForKey:@"sessionTypes" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeSessionTypes:(NSSet *)value {
    [self willChangeValueForKey:@"sessionTypes" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sessionTypes"] minusSet:value];
    [self didChangeValueForKey:@"sessionTypes" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


- (void)addSessionsObject:(Session *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sessions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sessions"] addObject:value];
    [self didChangeValueForKey:@"sessions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeSessionsObject:(Session *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"sessions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"sessions"] removeObject:value];
    [self didChangeValueForKey:@"sessions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addSessions:(NSSet *)value {    
    [self willChangeValueForKey:@"sessions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sessions"] unionSet:value];
    [self didChangeValueForKey:@"sessions" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeSessions:(NSSet *)value {
    [self willChangeValueForKey:@"sessions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"sessions"] minusSet:value];
    [self didChangeValueForKey:@"sessions" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}



-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    
    
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
    [dateFormatter setDateFormat:@"MM/dd/yyyy' 'HH:mm:ss"];

    

 
    
    
    
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{
    
    if (  [elementName isEqualToString:@"conference"] ) {
        self.id = [NSNumber numberWithInt:[[attributeDict objectForKey:@"id"] intValue]];
        self.name = [NSString stringWithString:[attributeDict objectForKey:@"name"]];
        self.thumbnailURL = [NSString stringWithString:[attributeDict objectForKey:@"xlink:logo"]];
        self.thumbnailData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.thumbnailURL]];
        return;
    }
    
    else if ( [elementName isEqualToString:@"coordinate"] ) {
        
        self.location.lat = [NSNumber numberWithFloat:[[attributeDict objectForKey:@"lat"] floatValue]];
        self.location.lon = [NSNumber numberWithFloat:[[attributeDict objectForKey:@"lon"] floatValue]];
        
        return;
    }
    else if ( [elementName isEqualToString:@"conference-track"] ) {
        
        Track *newTrack = [[NSEntityDescription insertNewObjectForEntityForName:@"Track" inManagedObjectContext:[self managedObjectContext]] retain];
        [[newTrack initWithParser:[self xmlParser] attributeDict:attributeDict parent:self] autorelease];
        [self addTracksObject:newTrack];
        [newTrack release];
        
        return;
    }
    else if ( [elementName isEqualToString:@"conference-session-type"] ) {
        SessionType *newSessionType = [[NSEntityDescription insertNewObjectForEntityForName:@"SessionType" inManagedObjectContext:[self managedObjectContext]] retain];
        [[newSessionType initWithParser:[self xmlParser] attributeDict:attributeDict parent:self] autorelease];
        [self addSessionTypesObject:newSessionType];
        [newSessionType release];
        
        return;
    }
    else if ( [elementName isEqualToString:@"conference-sponsorship-level"] ) {
        
        SponsorshipLevel *newEntry = [[NSEntityDescription insertNewObjectForEntityForName:@"SponsorshipLevel" inManagedObjectContext:[self managedObjectContext]] retain];
        [[newEntry initWithParser:[self xmlParser] attributeDict:attributeDict parent:self] autorelease];
        [self addSponsorshipLevelsObject:newEntry];
        [newEntry release];
        
        return;
    }
    else if ( [elementName isEqualToString:@"conference-venue"] ) {
        
        Venue *newVenue = [[NSEntityDescription insertNewObjectForEntityForName:@"Venue" inManagedObjectContext:[self managedObjectContext]] retain];
        [[newVenue initWithParser:[self xmlParser] attributeDict:attributeDict parent:self] autorelease];
        [self addVenuesObject:newVenue];
        [newVenue release];
        
        return;
    }
    else if ( [elementName isEqualToString:@"conference-speaker"] ) {
        
        Speaker *newSpeaker = [[NSEntityDescription insertNewObjectForEntityForName:@"Speaker" inManagedObjectContext:[self managedObjectContext]] retain];
        [[newSpeaker initWithParser:[self xmlParser] attributeDict:attributeDict parent:self] autorelease];
        [self addSpeakersObject:newSpeaker];
        [newSpeaker release];
        
        return;
    }
    else if ( [elementName isEqualToString:@"conference-session"] ) {
        
        Session *newEntry = [[NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:[self managedObjectContext]] retain];
        [[newEntry initWithParser:[self xmlParser] attributeDict:attributeDict parent:self] autorelease];
        [self addSessionsObject:newEntry];
        [newEntry release];
        
        return;
    }
    else if ( [elementName isEqualToString:@"conference-sponsor"] ) {
        
        Sponsor *newSponsor = [[NSEntityDescription insertNewObjectForEntityForName:@"Sponsor" inManagedObjectContext:[self managedObjectContext]] retain];
        [[newSponsor initWithParser:[self xmlParser] attributeDict:attributeDict parent:self] autorelease];
            //      [newSponsor initWithParser:[self xmlParser] attributeDict:attributeDict parent:self];
        [self addSponsorsObject:newSponsor];
        [newSponsor release];
        
        return;
    }
    
    
}



- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    
    
    string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        //  NSLog(@"Found Chars : %@\t", string);
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@" & "];
        // NSLog(@"Found Chars : %@\n", string);
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    if (!xmlStringBuilder) {
        xmlStringBuilder = [[NSMutableString alloc] initWithCapacity:100];
    }
        //   NSLog(@"XML String builder : %@\t", xmlStringBuilder);
    [xmlStringBuilder appendString:string];
        //NSLog(@"XML String builder : %@\n", xmlStringBuilder);
    
}

-(void)parser:(NSXMLParser *)parser foundIgnorableWhitespace:(NSString *)whitespaceString
{    
    
        //Didnt need this 
    /**
     
     NSLog(@"\nfound ignoreable whitespace: whitespace string: \"%@\"\n", whitespaceString);
     if (!xmlStringBuilder) {
     xmlStringBuilder = [[NSMutableString alloc] initWithCapacity:100];
     
     }
     //   if (whitespaceString != @"\n")
     // [xmlStringBuilder appendString:whitespaceString];
     
     */
    
}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
        // ignore root and empty elements
    if (( [elementName isEqualToString:@"location"]) ||
        ( [elementName isEqualToString:@"conference"] )) return;

    else if( [elementName isEqualToString:@"start-date"] ) {
        self.startDate = [dateFormatter dateFromString:xmlStringBuilder];
    }
    
    else if( [elementName isEqualToString:@"end-date"] ) {
        
        self.endDate = [dateFormatter dateFromString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"description"] ) {
        
        self.desc =[NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"email"] ) {
        self.email = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"website"] ) {
        self.website = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"twitter"] ) {
        self.twitter = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"address"] ) {
        self.location.address = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"city"] ) {
        self.location.city = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"province"] ) {
        self.location.province = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"country"] ) {
        self.location.country = [NSString stringWithString:xmlStringBuilder];
        
    }
    
    else if( [elementName isEqualToString:@"postalCode"] ) {
        self.location.postalCode = [NSNumber numberWithInt:[xmlStringBuilder intValue]];
        
    }
    
    
    xmlStringBuilder = nil;
    
    
    
}



-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    
    NSLog(@"XML ERROR :\n%@\n",[parseError description]);
    
    UIAlertView *parserAlert = [[UIAlertView alloc]init];
    
    
    [parserAlert setTitle:@"Parsing Error!"];
    
    [parserAlert setMessage:[NSString stringWithFormat:@"Error %i, Description: %@, Line: %i, Column: %i", 
                             [parseError code], 
                             [parseError localizedDescription], 
                             [parser lineNumber], 
                             [parser columnNumber]]];
    
    [parserAlert addButtonWithTitle:@"OK"];
    [parserAlert show];
    [parserAlert release];
    
    
    
}

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    
    [dateFormatter release];
}


@end

